home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / emacs-18.59src.lha / emacs-18.59 / amiga / contrib / lindgren / sas-c-emacs.lha / slashquote / slashquote.c next >
Encoding:
C/C++ Source or Header  |  1993-04-02  |  6.0 KB  |  262 lines

  1. /*
  2.  *  FILE
  3.  *    slashquote.c
  4.  *
  5.  *  DESCRIPTION
  6.  *    A one function ARexx function library.
  7.  *
  8.  *  AUTHOR
  9.  *    Anders Lindgren, d91ali@csd.uu.se
  10.  *
  11.  *  STATUS
  12.  *    slashquote is free software; you can redistribute it and/or modify
  13.  *    it under the terms of the GNU General Public License as published 
  14.  *    by the Free Software Foundation; either version 1, or (at your 
  15.  *    option) any later version.
  16.  *
  17.  *    GNU Emacs is distributed in the hope that it will be useful,
  18.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  *    GNU General Public License for more details.
  21.  *
  22.  *    You should have received a copy of the GNU General Public
  23.  *    License along with GNU Emacs; see the file COPYING.  If not,
  24.  *    write to the Free Software Foundation, 675 Mass Ave, Cambridge,
  25.  *    MA 02139, USA.
  26.  */
  27.  
  28. /* Arguments:
  29.  * Up to 15 arguments may be passed in the rm_Args[] array of the parameter
  30.  * block.  Arguments are ALWAYS passed as argstrings and can generally be
  31.  * treated like string pointers in a 'C' program.  The called function may
  32.  * need to convert the strings to numeric values if arithmetic operations
  33.  * are to be performed.  A NULL value in the argument slot means that the
  34.  * argument was omitted from the function call, as in MyFunc(1,,3).
  35.  *
  36.  * The total number of arguments (including the defaulted ones) is available
  37.  * in the low-order byte of the action code field rm_Action.
  38.  * Note that REXX supports function calls with varying numbers of arguments,
  39.  * and that the called function can always determine how many were actually
  40.  * passed.
  41.  *
  42.  * Error reporting:
  43.  * The function must return an integer error code and a result string if no
  44.  * errors were detected.  Errors are considered to be ARexx internal error
  45.  * codes, so the function should make use of these values as appropriate.
  46.  * A code of 0 is interpreted to mean that everything worked.
  47.  *
  48.  * Result strings:
  49.  * The result string must be returned as an argstring, a pointer to the
  50.  * string buffer of an RexxArg structure.  Argstrings can be created by a
  51.  * call to the ARexx Systems library function CreateArgstring().
  52.  * N.B. Never allocate a result string if the error code is non-zero!
  53.  */
  54.  
  55. #include <exec/types.h>
  56.  
  57. #include <exec/memory.h>
  58.  
  59. #include <rexx/storage.h>
  60. #include <rexx/rxslib.h>
  61. #include <rexx/errors.h>
  62.  
  63. #include <proto/exec.h>
  64. #include <proto/rexxsyslib.h>
  65.  
  66. #include <string.h>
  67. #include <dos.h>
  68.  
  69.  
  70. /*
  71.  *  Fuction described in ARexx User's Reference Manual.
  72.  */
  73.  
  74. long CVa2i(char *);
  75. #pragma libcall RexxSysBase CVa2i 12C 801
  76.  
  77.  
  78. /*
  79.  *  Forward references.
  80.  */
  81.  
  82. extern long slashquote (struct RexxMsg *, UBYTE * *);
  83. extern long zerot2str  (struct RexxMsg *, UBYTE * *);
  84.  
  85.  
  86. /*
  87.  *  Global data.
  88.  */
  89.  
  90. struct Library * RexxSysBase;
  91.  
  92.  
  93. /*
  94.  *  FUNCTION
  95.  *    __UserLibInit
  96.  *
  97.  *  DESCRIPTION
  98.  *    Open the rexx system library.
  99.  */
  100.  
  101. int __saveds
  102. __UserLibInit(void)
  103. {
  104.     if (RexxSysBase = OpenLibrary("rexxsyslib.library", 0)) {
  105.     return(0);
  106.     }
  107.     return(1);
  108. }
  109.  
  110.  
  111. /*
  112.  *  FUNCTION
  113.  *    __UserLibCleanup
  114.  *
  115.  *  DESCRIPTION
  116.  *    Close the Rexx system library
  117.  */
  118.  
  119. void __saveds
  120. __UserLibCleanup(void)
  121. {
  122.     if (RexxSysBase) {
  123.     CloseLibrary(RexxSysBase);
  124.     }
  125. }
  126.  
  127.  
  128. /*
  129.  *  FUNCTION
  130.  *    LIBRexxEntry
  131.  *
  132.  *  DESCRIPTION
  133.  *    The library entry point.
  134.  *    Check if this library contains the desired function, otherwise
  135.  *    return a errorcode 1.
  136.  */
  137.  
  138. long __asm __saveds
  139. LIBRexxEntry(register __a0 struct RexxMsg * rmptr)
  140. {
  141.     long        retcode = 1L;    /* Fail by default */
  142.     UBYTE *     retval  = NULL;
  143.  
  144.  
  145.     if (strcmp("SLASHQUOTE", rmptr->rm_Args[0]) == 0) {
  146.     retcode = slashquote(rmptr, & retval);
  147.     } 
  148.     else if (strcmp("ZEROT2STR", rmptr->rm_Args[0]) == 0) {
  149.     retcode = zerot2str(rmptr, & retval);
  150.     }
  151.  
  152.     /* Set the return values */
  153.     __builtin_putreg(REG_A0, (long)retval);    /* Return String    */
  154.     return(retcode);
  155. }
  156.  
  157.  
  158. /*
  159.  *  FUNCTION
  160.  *    slashquote
  161.  *
  162.  *  DESCRIPTION
  163.  *    Manipulates a string in the following way:
  164.  *      1) Put quotes around it.
  165.  *      2) Escape all quotes and escapecharacters in the string. The
  166.  *         escapecharacter is the 2:nd arg, or '\' if none is supplied.
  167.  */
  168.  
  169. long 
  170. slashquote(struct RexxMsg * rmptr, UBYTE * * retvalp)
  171. {
  172.     register char    ch;
  173.          char    qch;    /* Character to take care of. */
  174.     register UBYTE * dest;
  175.     register UBYTE * src;
  176.     register long    sindex = 0;
  177.     register long    dindex = 0;
  178.     register int     len;
  179.  
  180.     /* Check the number of arguments */
  181.     if ( (rmptr->rm_Action & 0xFF) == 1 ) {
  182.     qch = '\\';    /* Default, quote backslash */ 
  183.     }
  184.     else if ( (rmptr->rm_Action & 0xFF) == 2 ) {
  185.     qch = rmptr->rm_Args[2][0];
  186.     }
  187.     else {
  188.     return(ERR10_017);
  189.     }
  190.  
  191.     len = LengthArgstring(rmptr->rm_Args[1]);
  192.  
  193.     if (dest = AllocMem((len<<1) + 2, MEMF_CLEAR)) {
  194.  
  195.     dest[dindex++] = '"';    /* Start quote */
  196.  
  197.     src = rmptr->rm_Args[1];
  198.  
  199.     do {
  200.         ch = src[sindex++];
  201.  
  202.         if (ch == '"' || ch == qch ) {
  203.         dest[dindex++] = qch;
  204.         }
  205.         dest[dindex++] = ch;
  206.     } while (ch);
  207.  
  208.     dest[dindex-1] = '"';    /* End quote */
  209.  
  210.     (* retvalp) = CreateArgstring(dest, dindex);
  211.  
  212.     FreeMem(dest, (len<<1) + 2);
  213.  
  214.         /* ERR10_003 "Out of memory" */
  215.     return( (* retvalp) ? 0L : ERR10_003 );
  216.     }
  217.     return(ERR10_003);
  218. }
  219.  
  220.  
  221. /*
  222.  *  FUNCTION
  223.  *    zerot2str
  224.  *
  225.  */
  226.  
  227.  
  228. long 
  229. zerot2str(struct RexxMsg * rmptr, UBYTE * * retvalp)
  230. {
  231.     register int slot;
  232.     register struct RexxMsg * pkt;
  233.  
  234.     if ( (rmptr->rm_Action & 0xFF) == 1 ) {
  235.     slot = 0;
  236.     }
  237.     else if ( (rmptr->rm_Action & 0xFF) == 2 ) {
  238.     slot = CVa2i(rmptr->rm_Args[2]);
  239.     }
  240.     else {
  241.     return(ERR10_017);
  242.     }
  243.  
  244.     /* The pkt must be a four bytes packed address */
  245.     if (LengthArgstring(rmptr->rm_Args[1]) != 4) {
  246.     return(ERR10_018);  /* Invalid arguments to function */
  247.     }
  248.  
  249.     /* Don't try to handle null-packets */
  250.     if ( (pkt = * ((struct RexxMsg **) rmptr->rm_Args[1])) == NULL ) {
  251.     return(ERR10_018);  /* Invalid arguments to function */
  252.     }
  253.  
  254.     if ((pkt->rm_Action & 0xFF) < slot) {
  255.     return(ERR10_018);  /* Invalid arguments to function */
  256.     }
  257.  
  258.     (* retvalp)=CreateArgstring(pkt->rm_Args[slot],strlen(pkt->rm_Args[slot]));
  259.  
  260.     return(0L);
  261. }
  262.